package com.zhku.xxl.util;

import com.zhku.xxl.entity.Admin;
import com.zhku.xxl.entity.AlumniAdmin;
import com.zhku.xxl.entity.User;
import com.zhku.xxl.service.AdminService;
import com.zhku.xxl.service.AlumniAdminService;
import com.zhku.xxl.service.GenericService;
import com.zhku.xxl.service.UserService;
import com.zhku.xxl.service.serviceImp.AdminServiceImp;
import com.zhku.xxl.service.serviceImp.AlumniAdminServiceImp;
import com.zhku.xxl.service.serviceImp.UserServiceImp;

public class Method {

    private static AdminService adminService = new AdminServiceImp();
    private static UserService userService = new UserServiceImp();
    private static AlumniAdminService alumniAdminService = new AlumniAdminServiceImp();

    public static Object checkPassword(String username, String password) {

        Admin admin = adminService.findByAccountAndPassword(Admin.class, username, password, username);

        if (admin != null) {

            return admin;
        }
        AlumniAdmin alumniAdmin = alumniAdminService.findByAccountAndPassword(AlumniAdmin.class, username, password, username);

        if (alumniAdmin != null) {

            return alumniAdmin;
        }

        User user = userService.findByAccountAndPassword(User.class, username, password, username);

        if (user != null) {

            return user;
        }

        return null;
    }


    /*
     * */

    public static int updateInfo(Class<?> en, String name, String contact, String email, String passwordFieldText, int Id) {
          return  new  GenericService().updateInfo(en,name,contact,email,passwordFieldText,Id);
    }

    public static Object getLastObject(String account) {
        Admin admin = adminService.findByAccount(account);

        if (admin == null) {
            AlumniAdmin alumniAdmin = alumniAdminService.findByAccount(account);

            if (alumniAdmin == null) {
                User user = userService.findByAccount(account);

                if (user == null) {
                    return null;
                }
                return  user;

            } else return alumniAdmin;
        }

        return admin;
    }

    public static String getImgUrl(String account) {
        Admin admin = adminService.findByAccount(account);

        if (admin == null) {

            User user = userService.findByAccount(account);
            if (user == null) {
                AlumniAdmin alumniAdmin = alumniAdminService.findByAccount(account);

                if (alumniAdmin == null) {
                    return null;
                }
                return alumniAdmin.getImageUrl();

            } else return user.getImageUrl();
        }

        return admin.getImageUrl();
    }
}
